package com.shuqi.model.parse.parser;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import com.shuqi.model.bean.PayAlipayInfo;
import com.shuqi.model.bean.PayFormInfo;
import com.shuqi.model.bean.PayItemInfo;
import com.shuqi.model.bean.PayTypeInfo;
import com.shuqi.model.parse.HandlerBase;

/**
 * 支付宝解析器
 * 
 * @author chenwei
 * 
 */
public class PayAlipayParser extends HandlerBase {

    private PayAlipayInfo payAlipayInfo;

    private String currentName;

    @Override
    public Object getParsedData() {
        return payAlipayInfo;
    }

    @Override
    public void startDocument() throws SAXException {
        super.startDocument();
        payAlipayInfo = new PayAlipayInfo();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes)
            throws SAXException {
        if (localName.equals("Alipay")) {
            payAlipayInfo.setState(isNull(attributes, "state"));
            payAlipayInfo.setMessage(isNull(attributes, "message"));
        } else if (localName.equals("PayInfo")) {
            currentName = localName;
        } else if (localName.equals("Recharge")) {
            payAlipayInfo.setCode(isNull(attributes, "code"));
            payAlipayInfo.setMsg(isNull(attributes, "msg"));
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        super.characters(ch, start, length);

        String s = new String(ch, start, length);
        if ("PayInfo".equals(currentName)) {
            payAlipayInfo.setPayInfo(s);
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        super.endElement(uri, localName, qName);
        if (localName.equals("OrderId")) {
            currentName = null;
        }
    }

    @Override
    public void endDocument() throws SAXException {
        super.endDocument();
    }
}
